package dao;

import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import util.Database;
import entity.Movie;
import entity.Pagination;

public class MovieDao {
	
public ArrayList<Movie> findAll(Pagination page){
		
		int totalRow = 0;
		int totalPage = 0;
		
		String sql ="select count(*) from movie"; 
		
		ResultSet rs_count = Database.query(sql);
		try {
			if (rs_count.next()) {
				totalRow = rs_count.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		totalPage = (int)Math.ceil(1.0*totalRow/page.getPageSize());
		page.setTotalRow(totalRow);
		page.setTotalPage(totalPage);
		
		ArrayList<Movie> list = new ArrayList<Movie>();
		sql = "select * from movie limit "+((page.getCurrentPage()-1)*page.getPageSize())+","+page.getPageSize();
	    ResultSet rs = Database.query(sql);
	    
	    try {
			while (rs.next()) {
				String id_string = rs.getString("mid");
				String name = rs.getString("name");
				String type = rs.getString("type");
				String actor = rs.getString("actor");
				String length_string = rs.getString("length");
				String director = rs.getString("director");
				String price_string = rs.getString("price");
				String mark_string = rs.getString("mark");
				String introduce = rs.getString("introduce");
				String picture = rs.getString("picture");
				
				int id = Integer.parseInt(id_string);
				int length = Integer.parseInt(length_string);
				float price = Float.parseFloat(price_string);
				float mark = Float.parseFloat(mark_string);
				
				Movie m = new Movie();
				m.setMid(id);
				m.setName(name);			
				m.setType(type);
				m.setActor(actor);
				m.setLength(length);
				m.setDirector(director);
				m.setPrice(price);
				m.setMark(mark);
				m.setIntroduce(introduce);
				m.setPicture(picture);
				
				list.add(m);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}


public ArrayList<Movie>findFive(){
	ArrayList<Movie> list = new ArrayList<Movie>();
	String sql = "select * from movie ORDER BY mark DESC LIMIT 0,5";
	ResultSet rs = Database.query(sql);
    try {
		while (rs.next()) {
			String id_string = rs.getString("mid");
			String name = rs.getString("name");
			String type = rs.getString("type");
			String actor = rs.getString("actor");
			String length_string = rs.getString("length");
			String director = rs.getString("director");
			String price_string = rs.getString("price");
			String mark_string = rs.getString("mark");
			String introduce = rs.getString("introduce");
			String picture = rs.getString("picture");
			
			int id = Integer.parseInt(id_string);
			int length = Integer.parseInt(length_string);
			float price = Float.parseFloat(price_string);
			float mark = Float.parseFloat(mark_string);
			
			Movie m = new Movie();
			m.setMid(id);
			m.setName(name);			
			m.setType(type);
			m.setActor(actor);
			m.setLength(length);
			m.setDirector(director);
			m.setPrice(price);
			m.setMark(mark);
			m.setIntroduce(introduce);
			m.setPicture(picture);
			
			list.add(m);
		}
	} catch (SQLException e) {
		e.printStackTrace();
	}
	return list;
	
	
}


public void add(Movie movie){
	String name = movie.getName();
	String type = movie.getType();
	String actor = movie.getActor();
	int length = movie.getLength();
	String director = movie.getDirector();
	float price = movie.getPrice();
	float mark = movie.getMark();
	String introduce = movie.getIntroduce();
	String picture = movie.getPicture();
	
	String sql= "insert into movie(name,type,actor,length,director,price,mark,introduce,picture) values('"+name+"','"+type+"','"+actor+"','"+length+"','"+director+"','"+price+"','"+mark+"','"+introduce+"','"+picture+"')";
	Database.execute(sql);
	
}

public void update(Movie movie,String mid) {
	String name = movie.getName();
	String type = movie.getType();
	String actor = movie.getActor();
	int length = movie.getLength();
	String director = movie.getDirector();
	float price = movie.getPrice();
	float mark = movie.getMark();
	String introduce = movie.getIntroduce();
	System.out.println(movie.getName());
	String sql= "update movie set name='"+name+"',type='"+type+"',actor='"+actor+"',length='"+length+"',director='"+director+"',price='"+price+"',mark='"+mark+"',introduce='"+introduce+"' where mid ='"+mid+"'";
	Database.execute(sql);
}


public ArrayList<Movie>findOne(int idd){
	ArrayList<Movie> list = new ArrayList<Movie>();
	String sql = "select * from movie where mid='"+ idd + "'";
	ResultSet rs = Database.query(sql);
    try {
		while (rs.next()) {
			String id_string = rs.getString("mid");
			String name = rs.getString("name");
			String type = rs.getString("type");
			String actor = rs.getString("actor");
			String length_string = rs.getString("length");
			String director = rs.getString("director");
			String price_string = rs.getString("price");
			String mark_string = rs.getString("mark");
			String introduce = rs.getString("introduce");
			String picture = rs.getString("picture");
			
			int id = Integer.parseInt(id_string);
			int length = Integer.parseInt(length_string);
			float price = Float.parseFloat(price_string);
			float mark = Float.parseFloat(mark_string);
			
			Movie m = new Movie();
			m.setMid(id);
			m.setName(name);			
			m.setType(type);
			m.setActor(actor);
			m.setLength(length);
			m.setDirector(director);
			m.setPrice(price);
			m.setMark(mark);
			m.setIntroduce(introduce);
			m.setPicture(picture);
			
			list.add(m);
		}
	} catch (SQLException e) {
		e.printStackTrace();
	}
	return list;
}

public ArrayList<Movie>find(String thing){
	ArrayList<Movie> list =new ArrayList<Movie>();
	
	String sql = "select * from movie where name like '%"+ thing + "%' or actor like '%"+ thing + "%' or director like '%"+ thing + "%' or type like '%"+ thing + "%'";
	/*Movie m =new Movie();
	m.setActor(sql);
	list.add(m);*/
	ResultSet rs = Database.query(sql);
    try {
		while (rs.next()) {
			String id_string = rs.getString("mid");
			String name = rs.getString("name");
			String type = rs.getString("type");
			String actor = rs.getString("actor");
			String length_string = rs.getString("length");
			String director = rs.getString("director");
			String price_string = rs.getString("price");
			String mark_string = rs.getString("mark");
			String introduce = rs.getString("introduce");
			String picture = rs.getString("picture");
			
			int id = Integer.parseInt(id_string);
			int length = Integer.parseInt(length_string);
			float price = Float.parseFloat(price_string);
			float mark = Float.parseFloat(mark_string);
			
			Movie m = new Movie();
			m.setMid(id);
			m.setName(name);			
			m.setType(type);
			m.setActor(actor);
			m.setLength(length);
			m.setDirector(director);
			m.setPrice(price);
			m.setMark(mark);
			m.setIntroduce(introduce);
			m.setPicture(picture);
			
			list.add(m);
		}
	} catch (SQLException e) {
		e.printStackTrace();
	}
	return list;
}

public void purchase(String mid,String uid) {
		int mid1 = Integer.parseInt(mid);
		int uid1 = Integer.parseInt(uid);
		String sql = "insert into purchase (mid,id) value('"+mid1+"','"+uid1+"')";
		Database.execute(sql);
	}

public void delete(String mid) {
	String sql = "delete from movie where mid = '"+mid+"'";
	Database.execute(sql);
}

}